import { MysqlClient } from "./common";
import mysql from "mysql";

export class PlayerModel {

    async getPlayer() {
        const res = await MysqlClient.Ins.select('game_player',"*", {
            where: {
                'player_id': 10001,
                'init_skin': ['>=',0],
            },
            orderBy: [['player_id', 'asc']],
            limit: 1
        });
        return res;
    }

    async addAdminConf() {
        const datas = [
            {
                'conf_name': 'test1',
                'conf_json': '111'
            },
            {
                'conf_name': 'test2',
                'conf_json': '222'
            }
        ];
        const res = await MysqlClient.Ins.insert('game_admin_conf', datas);
        return res;
    }

    async updateAdminConf() {
        const data = {
            'conf_json': "666"
        };
        return await MysqlClient.Ins.update('game_admin_conf', data, {
            where: {
                'id':3,
                'conf_name': ['!=' ,'test']
            },
            limit:1
        });
    }

    async tx() {
        const client = MysqlClient.Ins;
        const final = await client.transaction(async (conn: mysql.PoolConnection) => {
            const res = await client.find('game_admin_conf', "*", {
                where: {
                    'id': 3
                },
                limit: 1,
            },conn);
            const data = {
                'conf_json': "666"
            };
            const success = await client.update('game_admin_conf', data, {
                where: {
                    'id':res.id
                },
                limit:1
            }, conn);
            if(success.affectedRows == 0) {
                throw new Error('update error');
            }
            return res;
        });
        console.log(final)
    }
}